import React from 'react'

import ReportContent from "../../components/Report";
import {secs2time, ts2time} from "../../utils/utils";
import moment from "moment";
import {fetchRunReportData} from "../../services/report";

function getDistance(lat1, lng1, lat2, lng2) {
  const radLat1 = lat1 * Math.PI / 180.0;
  const radLat2 = lat2 * Math.PI / 180.0;
  const a = radLat1 - radLat2;
  const b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
    Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137;// EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  return s;
}

const handleDataFormat = (data, type) => {
    console.log(data);
    if (!data) return;
    if (type === 'detail') {

      return data.reduce((result, item) => {
        const {device_id, duration, end_address, end_lat, end_lng, end_name, end_time, id, speed, start_address, start_lat, start_lng, start_name, start_time, distance} = item;
        if(speed >= 1) {
            result.push(
                {
                    device_id,
                    speed,
                    start_time: moment(start_time * 1e3).format('YYYY-MM-DD hh:mm:ss'),
                    end_time: moment(end_time * 1e3).format('YYYY-MM-DD hh:mm:ss'),
                    duration: secs2time(duration * 1e3),
                    mileage_total: (distance / 1e3).toFixed(2),
                    start_xy: {lng: start_lng, lat: start_lat},
                    end_xy: {lng: end_lng, lat: end_lat},
                    start_address,
                    end_address,
                }
            )
        }
          return result
      },[])
    }
    let idx=0;
    return data.reduce((result, item) => {
      const {
        device_id,
        duration,
        mileage,
        end_stamp,
        start_stamp,
      } = item;
      const speed_avg = parseInt(mileage / duration * 3.6)
      if(speed_avg >= 1) {
          result.push( {
              key: idx++,
              device_id,
              mileage: parseInt(mileage / 1000),
              duration: secs2time(duration * 1e3),
              start_time: moment(start_stamp).format('YYYY-MM-DD hh:mm:ss'),
              end_time: moment(end_stamp).format('YYYY-MM-DD hh:mm:ss'),
              speed_avg,
          })
      };
      return result
    },[])

};


// # 设备编号  开始时间 结束时间 行驶里程 行驶时间 平均速度 行驶明细
const columnsDay = (handleDetailClick) => ([
  {
    dataIndex: "device_id",
    title: "设备编号"
  },
  {
    dataIndex: "start_time",
    title: "开始时间"
  },
  {
    dataIndex: "end_time",
    title: "结束时间"
  },
  {
    dataIndex: "mileage",
    title: "运行里程(km)"
  },
  {
    dataIndex: "duration",
    title: "运行时间"
  },
  {
    dataIndex: "speed_avg",
    title: "平均速度(km/h)"
  },
  {
    title: '操作',
    dataIndex: '',
    key: 'x',
    render: (text, record, index) => <a href="javascript:"
                                        onClick={handleDetailClick(text, record)}>明细</a>,
  },
]);

// # 设备编号 定位时间 速度 里程 行驶里程 位置
const columnsDetail = (handleTableClick) => ([
  {
    dataIndex: "device_id",
    title: "设备编号"
  },
  {
    dataIndex: "start_time",
    title: "开始时间"
  },
  {
    dataIndex: "end_time",
    title: "结束时间"
  },
  {
    dataIndex: "duration",
    title: "行驶时间"
  },
  {
    dataIndex: "mileage_total",
    title: "行驶里程(km)"
  },
  {
    dataIndex: "speed",
    title: "行驶速度(km/h)"
  },
  {
    dataIndex: "start_address",
    title: "开始位置",
    render: (text, record, index) => (
      <span>
            <a href="javascript:" onClick={handleTableClick(record.start_xy, index)}>{text}</a>
          </span>
    ),
  }, {
    dataIndex: "end_address",
    title: "结束位置",
    render: (text, record, index) => (
      <span>
            <a href="javascript:" onClick={handleTableClick(record.end_xy, index)}>{text}</a>
          </span>
    ),
  },
]);

const Run = (props) => (
  <ReportContent {...props} title="运行报表" fetch={fetchRunReportData} handleDataFormat={handleDataFormat}
                 columnsDay={columnsDay}
                 columnsDetail={columnsDetail}
                 showPolyline={true}
  />);


export default Run;
